package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DateUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.DatePopup;

/**
 * 作者：Administrator on 2017/7/25 15:44
 * 邮箱：zhanghuaiha@gmail.com
 * 日期选着
 */

public class DateActivity extends BaseActivity{

    private ImageView iv_left;
    private ImageView iv_right;
    private TextView tv_date;
    private TextView tv_week;
    private TextView tv_today;
    private TextView save;
    private DatePopup monthDateView;
    private String start;
    private ImageView back;
    private String end;
    private String string;
    private String new_date;

    @Override
    public void initView() {
        monthDateView = (DatePopup) findViewById(R.id.monthDateView);
        tv_date = (TextView) findViewById(R.id.date_text);
        tv_week  =(TextView) findViewById(R.id.week_text);
        save  =(TextView) findViewById(R.id.save);
        iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        back = (ImageView) findViewById(R.id.history_back);
        tv_today = (TextView) findViewById(R.id.tv_today);
        monthDateView.setTextView(tv_date,tv_week);
        int day = getIntent().getIntExtra("day", -1);
        if (day != -1) {
            monthDateView.setDay(day);
        }

        new_date = getIntent().getStringExtra("new_date");
    }

    @Override
    public void initListener() {
        back.setOnClickListener(v -> finish());

        monthDateView.setDateClick(() -> {
            if (monthDateView.getmSelMonth() < 10) {
                if (monthDateView.getmSelDay() < 10) {
                    string = monthDateView.getmSelYear() + "-0" + monthDateView.getmSelMonth() + "-0" + monthDateView.getmSelDay();
                }else {
                    string = monthDateView.getmSelYear() + "-0" + monthDateView.getmSelMonth() + "-" + monthDateView.getmSelDay();
                }
            }else {
                if (monthDateView.getmSelDay() < 10) {
                    string = monthDateView.getmSelYear() + "-" + monthDateView.getmSelMonth() + "-0" + monthDateView.getmSelDay();
                }else {
                    string = monthDateView.getmSelYear() + "-" + monthDateView.getmSelMonth() + "-" + monthDateView.getmSelDay();
                }
            }

        });

        iv_left.setOnClickListener(v -> {
            monthDateView.onLeftClick();
        });
        iv_right.setOnClickListener(v -> {
            monthDateView.onRightClick();
        });

        save.setOnClickListener(v -> {


            Intent intent = new Intent();
            if (string == null) {
                string = DateUtils.getTime(System.currentTimeMillis());
            }

            long l = DateUtils.S1compareS2(string, new_date, "yyyy-MM-dd");
            if (l > 0) {
                ToastUtil.showToast("请选择正确时间");
                return;
            }
            intent.putExtra("time",string);
            intent.putExtra("day",monthDateView.getmSelDay());
            setResult(101,intent);
            finish();
        });

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.acitivity_time;
    }
}
